<?php
namespace app\teacher\controller;

use app\teacher\controller\PublicController;
use app\common\logic\admin\Reviews;
use app\common\logic\home\Mytask;
use app\common\logic\home\Relation;
use app\common\model\AnserModel;
use app\common\model\TemplateGroupModel;
use app\common\model\ReviewModel;
use app\common\model\MytaskModel;
//模板发放
class ReviewController extends PublicController
{

    public function __construct(){
        parent::__construct();
        $this->check();
    }

    public function index1(Reviews $Reviews){
        $param = $this->request->param();

        $param['add_status'] = 2;
		$param['user_id'] = $this->user_id;
       	$data =  $Reviews->getList($param);
       	//dump($data);
       	$this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch('teacher@:review/index_1');
    }
    

    public function index2(Reviews $Reviews){
        $param = $this->request->param();
        $param['add_status'] = 1;
		$param['user_id'] = $this->user_id;
       	$data =  $Reviews->getList($param);
       
       	$this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch('teacher@:review/index_2');
    }

    public function index3(Reviews $Reviews){
        $param = $this->request->param();
        $param['add_status'] = 3;
		$param['user_id'] = $this->user_id;
       	$data =  $Reviews->getList($param);
       	$this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch('teacher@:review/index_3');
    }
    /**
     * [课题详情]
     * @param  Mytask $Mytask [description]
     * @return [type]         [description]
     */
    public function info(Mytask $Mytask){
    	$param = $this->request->param();
    	$data = $Mytask->index($param['mytask_id'],$param['user_id']);
    	//halt($datas);
    	$data_template = TemplateGroupModel::get($param['group_id']);
   		$this->assign('data_template',$data_template);
    	$this->assign('data',$data);
        return $this->fetch('teacher@:review/info');
    }

    /**
     * [老师点评]
     * @return [type] [description]
     */
    public function evaluate(){
    	$param = $this->request->param();
    	$where = [
    			'taskid'=>$param['taskid'],
    			'userId'=>$param['userId'],
    			'classprogressid'=>$param['classprogressid'],
    		];
    	$evaluate=AnserModel::where($where)->value('evaluate');
    	$this->assign('evaluate',$evaluate);

    	return $this->fetch('teacher@:review/evaluate');
    }
    /**
     * [提交老师点评]
     * @return [type] [description]
     */
    public function toEvaluate(Relation $Relation){
    	$param = $this->request->param();
    	//halt($param);
    	$mytask_ids = $Relation->getGrouplist($param['mytask_id'],$param['user_id']);

    	foreach ($mytask_ids as $key => $value) {

    		$rs = AnserModel::where('taskid',$value)->where('userId',$key)->where('classprogressid',$param['classprogressid'])->update(['evaluate'=>$param['evaluate']]);
    		//$rs = AnserModel::where('taskid',$value)->where('userId',$key)->where('classprogressid',$param['classprogressid'])->update(['evaluate'=>$param['evaluate']]);
    		
    	}
    	if($rs){
    		$this->success('成功','info?mytask_id='.$param['mytask_id'].'&user_id='.$param["user_id"].'&group_id='.$param['group_id']);
    	}else{
    		$this->error('学生未做回答,或评语重复导致失败');
    	}
    }
    /**
     * [课题驳回]
     * @param  Relation $Relation [description]
     * @return [type]             [description]
     */
    public function refuse(Relation $Relation){
        $param = $this->request->param();
        //dump($param);
        $mytask_ids = $Relation->getGrouplist($param['mytask_id'],$param['user_id']);
        $rs = ReviewModel::where('mytask_id',$param['mytask_id'])->where('refuse_time',NULL)->update(['refuse_time'=>date('Y-m-d H:i:s')]);
        //halt($rs);
        foreach ($mytask_ids as $key => $value) {
            $rs = MytaskModel::where('id',$value)->update(['add_status'=>1]);
        }
        if($rs){
            $this->success('成功');
        }else{
            $this->error('失败');
        }

    }

    public function play(){
        $param = $this->request->param();
        return $this->fetch('teacher@:review/play');
        
    }

    public function toPlay(Reviews $Reviews){
    	$param = $this->request->param();
    	$rs = $Reviews->agree($param['mytask_id'],$param['user_id'],$param['evaluate']);
    	if($rs){
    		$this->success('成功','index1');
    	}else{
    		$this->error('失败');
    	}
    }
}
